package com.lingjtx.auth.config;

import com.lingjtx.auth.handle.AuthHandle;
import com.lingjtx.auth.handle.UserTypeContext;
import com.lingjtx.common.security.limit.ApiContext;
import com.lingjtx.common.security.strategy.LoginPolicy;
import com.lingjtx.common.security.strategy.LogoutPolicy;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserTypeContextWrapper {

    private final LoginPolicy loginPolicy;
    private final LogoutPolicy logoutPolicy;
    private final UserTypeContext userTypeContext;
    private final ObjectProvider<ApiContext<ProceedingJoinPoint, HttpServletRequest>> apiContextProvider;

    private final AuthHandle tocAuthService;
    private final AuthHandle tobAuthService;
    private final AuthHandle developerAuthService;
    private final AuthHandle systemAuthService;

    public UserTypeContextWrapper(LoginPolicy loginPolicy, LogoutPolicy logoutPolicy, UserTypeContext userTypeContext,
                                  @Qualifier("loginOrLogoutContext") ObjectProvider<ApiContext<ProceedingJoinPoint, HttpServletRequest>> apiContextProvider,
                                  @Qualifier("tocAuthService") AuthHandle tocAuthService,
                                  @Qualifier("tobAuthService") AuthHandle tobAuthService,
                                  @Qualifier("developerAuthService") AuthHandle developerAuthService,
                                  @Qualifier("systemAuthService") AuthHandle systemAuthService) {
        this.loginPolicy = loginPolicy;
        this.logoutPolicy = logoutPolicy;
        this.userTypeContext = userTypeContext;
        this.apiContextProvider = apiContextProvider;
        this.tocAuthService = tocAuthService;
        this.tobAuthService = tobAuthService;
        this.developerAuthService = developerAuthService;
        this.systemAuthService = systemAuthService;
    }

    // 初始化一次
    @PostConstruct
    public void init() {
        userTypeContext.setHandlers(tocAuthService);
        userTypeContext.setHandlers(tobAuthService);
        userTypeContext.setHandlers(developerAuthService);
        userTypeContext.setHandlers(systemAuthService);
    }

    public AuthHandle getHandle(String type) {
        return userTypeContext.getHandle(type);
    }

    public ApiContext<ProceedingJoinPoint, HttpServletRequest> create(AuthHandle auth) {
        ApiContext<ProceedingJoinPoint, HttpServletRequest> context = apiContextProvider.getObject();
        loginPolicy.setCallback(auth);
        context.setHandlers(loginPolicy);

        logoutPolicy.setCallback(auth);
        context.setHandlers(logoutPolicy);
        return context;
    }
}
